package com.fzf.blog.modules.api.service.impl;

import com.fzf.blog.core.util.BeanUtil;
import com.fzf.blog.core.util.CopyFile;
import com.fzf.blog.modules.admin.mapper.CourseCatalogMapper;
import com.fzf.blog.modules.admin.mapper.ResourceMapper;
import com.fzf.blog.modules.admin.model.CourseCatalog;
import com.fzf.blog.modules.api.model.CourseCatalogVo;
import com.fzf.blog.modules.api.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseCatalogMapper courseCatalogMapper;


    @Override
    public List<CourseCatalogVo> selectByKcId(Integer kcId) {
        Example example = new Example(CourseCatalog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("kcId",kcId);
        List<CourseCatalog> list= courseCatalogMapper.selectByExample(example);
        List<CourseCatalogVo> listvo = new ArrayList<>();
        list.forEach(courseCatalog ->{
            Example ex = new Example(CourseCatalog.class);
            Example.Criteria cr = ex.createCriteria();
            cr.andEqualTo("parentId",courseCatalog.getId());
            List<CourseCatalog> co = courseCatalogMapper.selectByExample(ex);
            CourseCatalogVo courseCatalogVo = new CourseCatalogVo();
            BeanUtil.copyNotNullBean(courseCatalog,courseCatalogVo);
            courseCatalogVo.setChildren(co);
            listvo.add(courseCatalogVo);
        });
        return listvo;
    }
}
